﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.NewPBAuto.Presenter;
using MINERP.NewPBAuto.IView;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.NewPinBan;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Leadtools.WinForms;
using System.Diagnostics;
using MINERP.NewPBAuto.Forms;
using MINERP.ComControls;
using MINERP.NewPBAuto.com;
using System.Configuration;
using MINERP.COM;
using MINERP.BLL.Common;

namespace MINERP.NewPBAuto.Forms
{
    public partial class FrmMain : FrmParent, IMainView
    {
        private MainPresenter _presenter;
        bool HaveAPanel = false;//是否有多选图片的临时PANEL
        Panel KXP;//如果有则指向该PANEL
        bool MouseIsDown = false;//用于多选判断鼠标是否被按下
        Rectangle MouseRect = Rectangle.Empty;//将要画矩形框的大小
        List<PicView> selectedCtrls = new List<PicView>();//被 框选 的那些图片集合

        //private const int perPixel = 10;//新建纸时的每一英寸在电脑屏上等于多少个相素点;
        int perPixel=0;
        public FrmMain()
        {
            InitializeComponent();
            base.InitiAuthority();

            RushBoardStatus();
            //在没有选择模板前要初始化一个模块，主要用来一画刻度线
            DataTransfer.curTemplate = new Tb_paperTemplate_Info() { BoardHeight = 30, BoardLength = 40 };
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            perPixel = DataTransfer.perPix;
            pRedLine.BackColor = Color.Red;
            pRedLine.Width = 1;
            comboBox1.SelectedIndex = 0;//黙认为上方
            comboBox2.SelectedIndex = 0;//正常模式
            //从配置文件中获取需要获取的定单数量
            cmboxOrderNum.Items.Clear();
            cmboxOrderNum.Items.Add(ConfigurationManager.AppSettings["PBorderNum"]);
            cmboxOrderNum.Items.Add("全部");
            cmboxOrderNum.SelectedIndex = 0;
            try
            {
                DataTransfer.ShowOrderNum = Convert.ToInt32(ConfigurationManager.AppSettings["PBorderNum"]);
            }
            catch (Exception)
            {
                DataTransfer.ShowOrderNum = 30;
            }



            DataTransfer.curUid = base.userId;
            //DataTransfer.curUid = 32;
            DataTransfer.curUser = base.userName;
            _presenter = new MainPresenter(this);
            comboBoxSizeUnit.SelectedIndex = 0;
        }

        private void cmbTemplate_DropDownClosed(object sender, EventArgs e) //选择模板后
        {
            this.Cursor = Cursors.WaitCursor;

            if (!string.IsNullOrEmpty(cmbTemplate.Text))
            {
                DataTransfer.PreSelectTemplateName = SelectTemplateName;
                _presenter.SelectedTemplate();
                PnlBigBoard.Select();
                toolStripButton3.Enabled = true;
                toolStripButton4.Enabled = true;
            }

            this.Cursor = Cursors.Default;
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            DrawScaleLine();
            DrawRedLine();
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)//在商户、尺寸、清柜之前切换
        {

        }

        #region 拖拽主列表中的照片的操作

        GridHitInfo downHitInfo = null;
        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;

            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));

            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator)
                downHitInfo = hitInfo;
        }

        private void gridView1_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))//当他移出这个区域后就开始拖操作
                {
                    //DataObject data = new DataObject();
                    //data.SetData(DataFormats.FileDrop, GetDragData(view));
                    List<PhotoInfo_PinBan> SelectedPhotos = GetDragData(view);
                    //view.GridControl.DoDragDrop(data, DragDropEffects.Copy);
                    view.GridControl.DoDragDrop(SelectedPhotos, DragDropEffects.Copy);
                    downHitInfo = null;
                }
            }
        }

        private void gridView1_MouseUp(object sender, MouseEventArgs e)
        {
            downHitInfo = null;
        }

        private List<PhotoInfo_PinBan> GetDragData(GridView view)
        {
            List<PhotoInfo_PinBan> photoIdList = new List<PhotoInfo_PinBan>();
            foreach (var piId in view.GetSelectedRows())
            {
                PhotoInfo_PinBan pi = (PhotoInfo_PinBan)view.GetRow(piId);
                if (pi.IsInBoard == false)
                {
                    photoIdList.Add(pi);
                }
            }
            return photoIdList;
        }



        public void ClearBigBoard()
        {
            PnlBigBoard.Controls.Clear();
            gridView1.RefreshData();
            ShowWaste("100%", Color.Red);
        }

        public void RefushMainGrid()
        {
            gridView1.RefreshData();
        }
        #endregion

        #region 大纸上接收拖上来的图片

        private void PnlBigBoard_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<PhotoInfo_PinBan>)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
                e.Effect = DragDropEffects.None;
        }

        private void PnlBigBoard_DragDrop(object sender, DragEventArgs e)
        {
            List<PhotoInfo_PinBan> photoList = (List<PhotoInfo_PinBan>)e.Data.GetData(typeof(List<PhotoInfo_PinBan>));//接收到的照片实体
            //根据这些照片ID把他们放在应放的位置上
            Point currentPicLoacation = this.PnlBigBoard.PointToClient(new Point(e.X, e.Y));//currentPicLocation的从标为图片相对于大板的坐标
            //_presenter.DrawPic(photoList, currentPicLoacation);
            _presenter.NewDrawPic(photoList, currentPicLoacation);
        }
        #endregion

        private void gridView1_RowStyle(object sender, RowStyleEventArgs e)//改变颜色
        {
            if (e.RowHandle >= 0)
            {
                PhotoInfo_PinBan curPi = (PhotoInfo_PinBan)gridView1.GetRow(e.RowHandle);
                if (curPi.IsInBoard)
                {
                    e.Appearance.BackColor = Color.GreenYellow;
                }
                else
                {
                    if (curPi.Priority > 6)
                    {
                        e.Appearance.BackColor = Color.Orange;
                    }
                }
            }
        }

        private void 清空_Click(object sender, EventArgs e)
        {
            _presenter.ClearBigBoard();
        }

        private void timer1_Tick_1(object sender, EventArgs e)//定时绘制红线
        {
            //this.DrawRedLine();
            PnlBigBoard.Refresh();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)//是否显示图片
        {
            this.Cursor = Cursors.WaitCursor;
            DataTransfer.IsShowImg = checkBox1.Checked;
            _presenter.ShowImgChanged();
            this.Cursor = Cursors.Default;
        }

        private void LBShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LBShop.Focused)
            {
                _presenter.FilterShop();
            }
        }
        private void LBShop_AfterSelect(object sender, TreeViewEventArgs e)//筛选商户
        {
            if (LBShop.Focused)
            {
                _presenter.FilterShop();
            }
        }

        private void LBSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LBSize.Focused)
            {
                _presenter.FilterPhotoSize();
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //true是只看清柜的，false看全部的
            if (checkBox3.Checked)
            {
                _presenter.BindClearBoxList();
            }
            else
            {
                _presenter.RushBindPhotos(true,chkShowAllPaperTypePhotos.Checked);
            }

        }

        private void toolStripButton5_Click(object sender, EventArgs e)//打印
        {
            this.Cursor = Cursors.WaitCursor;
            toolStripButton5.Enabled = false;
            _presenter.PintCurBoard();
            toolStripButton5.Enabled = true;
            this.Cursor = Cursors.Default;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)//新建模板
        {
            _presenter.SelectTemplate();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)//刷新
        {
            this.Cursor = Cursors.WaitCursor;
            _presenter.RushBindPhotos(chkShowAllPaperTypePhotos.Checked);
            this.Cursor = Cursors.Default;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)//半自动拼板
        {
            this.Cursor = Cursors.WaitCursor;
            toolStripButton3.Enabled = false;
            try
            {
                _presenter.AutoPB();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                toolStripButton3.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)//全自动拼板
        {
            _presenter.FAutoPB();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //需要清理DataTransfer中的数据
            _presenter.ClearDataTransferData();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)//打开通迅模块
        {
            _presenter.ShowConnectionFrm();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)//转发图片
        {
            _presenter.SendToOtherPrinter();
        }


        #region 多选图片

        private void PnlBigBoard_MouseDown_1(object sender, MouseEventArgs e)
        {
            if (HaveAPanel)
            {

                //撤消多选
                _presenter.CancelMutilSelect();

                HaveAPanel = false;
            }

            MouseIsDown = true;
            DrawStart(e.Location);
        }

        private void PnlBigBoard_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (MouseIsDown)
            {
                e.Location.Offset(PnlBigBoard.Location);
                ResizeToRectangle(e.Location);
            }
        }

        private void PnlBigBoard_MouseUp_1(object sender, MouseEventArgs e)
        {
            int minX, minY, maxX, maxY;

            this.PnlBigBoard.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            MouseIsDown = false;

            DrawRectangle();
            MouseRect = rectCorrection(MouseRect);


            foreach (Control ct in PnlBigBoard.Controls)
            {
                //if ((ct is PicView) && (MouseRect.Contains(ct.Location) || MouseRect.Contains(new Point(ct.Location.X + ct.Width, ct.Location.Y + ct.Height))))
                if ((ct is PicView) && MouseRect.IntersectsWith(((PicView)ct).Rect))
                {
                    selectedCtrls.Add((PicView)ct);
                }
            }
            if (selectedCtrls.Count <= 1)
            {
                selectedCtrls.Clear();
                return;
            }

            minX = selectedCtrls.Min(c => c.Location.X);
            minY = selectedCtrls.Min(c => c.Location.Y);

            maxX = selectedCtrls.Max(c => c.Location.X + c.Width);
            maxY = selectedCtrls.Max(c => c.Location.Y + c.Height);


            PicView pv = new PicView(maxX - minX, maxY - minY, new Point(minX, minY));
            _presenter.MultiSelectedPic(pv, selectedCtrls);
            ShowPic(pv);

            HaveAPanel = true;

            MouseRect = Rectangle.Empty;
        }

        #region 框选的 help methods

        //修正MouseRect 的值
        Rectangle rectCorrection(Rectangle oldRect)
        {
            if (oldRect.Width < 0)
            {
                oldRect.X += oldRect.Width;
                oldRect.Width *= -1;
            }
            if (oldRect.Height < 0)
            {
                oldRect.Y += oldRect.Height;
                oldRect.Height *= -1;
            }
            return new Rectangle(oldRect.X, oldRect.Y, oldRect.Width, oldRect.Height);
        }

        private void ResizeToRectangle(Point p)
        {
            DrawRectangle();
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle();
        }
        private void DrawRectangle()
        {
            Rectangle rect = this.PnlBigBoard.RectangleToScreen(MouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.YellowGreen, FrameStyle.Dashed);
        }
        private void DrawStart(Point StartPoint)
        {
            this.PnlBigBoard.Capture = true;
            //Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
            Cursor.Clip = this.PnlBigBoard.RectangleToScreen(new Rectangle(0, 0, this.PnlBigBoard.Width, PnlBigBoard.Height));
            MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }
        #endregion

        #endregion

        #region IMainView 成员

        private string _selectShopName;
        public string SelectShop
        {
            get
            {
                _selectShopName = "";
                _selectCtmName = "";
                _selectOrderProductName = "";
                TreeNode selNode = LBShop.SelectedNode;
                if (selNode == null)
                {
                    return _selectShopName;
                }
                int level = (int)selNode.Tag;
                if (level == 1)//选择的是商户
                {
                    _selectShopName = selNode.Text.Trim();
                    return _selectShopName;
                }

                else if (level == 2)//选择的是客户
                {
                    _selectCtmName = selNode.Text.Trim();
                    _selectShopName = selNode.Parent.Text.Trim();
                    return _selectShopName;
                }
                else if (level == 3)//选择的是产品名称
                {
                    _selectCtmName = selNode.Parent.Text.Trim();
                    _selectOrderProductName = selNode.Text.Trim();
                    _selectShopName = selNode.Parent.Parent.Text.Trim();
                    return _selectShopName;
                }
                return null;
            }
        }

        public string SelectPhotoSize
        {
            get { return LBSize.SelectedItem.ToString(); }
        }

        public void ShowWaste(string strWaste, Color c)//显示浪费率
        {
            lblWate.Text = strWaste;
            lblWate.ForeColor = c;
        }

        public void DrawScaleLine()//画刻度线
        {
            //PnlBigBoard.Height = Convert.ToInt32(perPixel * DataTransfer.curTemplate);
            if (comboBoxSizeUnit.SelectedIndex == 1)
            {

                int startX = PnlBigBoard.Left;
                int startY = PnlBigBoard.Top;

                int currentX = startX;
                int currentY = startY;

                int num = 0;

                Graphics g = this.CreateGraphics();
                g.Clear(this.BackColor);
                Pen p = new Pen(Color.Black, 1);

                while (num * perPixel < PnlBigBoard.Width)
                {
                    switch (num % 4)
                    {
                        case 0:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 20);

                            Rectangle rect = new Rectangle(currentX + 1, currentY - 20, 25, 15);
                            Font aFont = new Font("Arial", 6, FontStyle.Regular);
                            g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                            aFont.Dispose();

                            break;
                        case 1:
                        case 3:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 8);
                            break;
                        case 2:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 12);
                            break;
                        default:
                            break;
                    }
                    num++;
                    currentX += Convert.ToInt32(perPixel);
                }

                currentX = startX;
                currentY = startY;
                num = 0;
                while (num * perPixel <= PnlBigBoard.Height)
                {
                    if (num * perPixel == PnlBigBoard.Height)
                    {
                        g.DrawLine(p, currentX, currentY, currentX - 20, currentY);

                        Rectangle rect = new Rectangle(currentX - 20 + 1, currentY + 1, 15, 15);
                        Font aFont = new Font("Arial", 6, FontStyle.Regular);
                        g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                        aFont.Dispose();
                    }
                    else
                    {
                        switch (num % 4)
                        {
                            case 0:
                                g.DrawLine(p, currentX, currentY, currentX - 20, currentY);

                                Rectangle rect = new Rectangle(currentX - 20 + 1, currentY + 1, 15, 15);
                                Font aFont = new Font("Arial", 6, FontStyle.Regular);
                                g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                                aFont.Dispose();

                                break;
                            case 1:
                            case 3:
                                g.DrawLine(p, currentX, currentY, currentX - 8, currentY);
                                break;
                            case 2:
                                g.DrawLine(p, currentX, currentY, currentX - 12, currentY);
                                break;
                            default:
                                break;
                        }
                    }
                    num++;
                    currentY += Convert.ToInt32(perPixel);
                }

                p.Dispose();
            }
            else
            {
                int startX = PnlBigBoard.Left;
                int startY = PnlBigBoard.Top;

                int currentX = startX;
                int currentY = startY;

                int num = 0;

                Graphics g = this.CreateGraphics();
                g.Clear(this.BackColor);
                Pen p = new Pen(Color.Black, 1);

                while (num * perPixel < PnlBigBoard.Width)
                {
                    switch (num % 4)
                    {
                        case 0:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 20);

                            Rectangle rect = new Rectangle(currentX + 1, currentY - 20, 30, 15);
                            Font aFont = new Font("Arial", 8, FontStyle.Regular);
                            g.DrawString((num*2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                            aFont.Dispose();

                            break;
                        case 1:
                        case 3:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 8);
                            break;
                        case 2:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 12);
                            break;
                        default:
                            break;
                    }
                    num++;
                    currentX += Convert.ToInt32(perPixel);
                }

                currentX = startX;
                currentY = startY;
                num = 0;
                while (num * perPixel <= PnlBigBoard.Height)
                {
                    if (num * perPixel == PnlBigBoard.Height)
                    {
                        g.DrawLine(p, currentX, currentY, currentX - 20, currentY);

                        Rectangle rect = new Rectangle(currentX - 20 + 1, currentY + 1, 30, 15);
                        Font aFont = new Font("Arial", 7, FontStyle.Regular);
                        g.DrawString((num * 2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                        aFont.Dispose();
                    }
                    else
                    {
                        switch (num % 4)
                        {
                            case 0:
                                g.DrawLine(p, currentX, currentY, currentX - 20, currentY);

                                Rectangle rect = new Rectangle(currentX - 20 + 1, currentY + 1, 30, 15);
                                Font aFont = new Font("Arial", 7, FontStyle.Regular);
                                g.DrawString((num*2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                                aFont.Dispose();

                                break;
                            case 1:
                            case 3:
                                g.DrawLine(p, currentX, currentY, currentX - 8, currentY);
                                break;
                            case 2:
                                g.DrawLine(p, currentX, currentY, currentX - 12, currentY);
                                break;
                            default:
                                break;
                        }
                    }
                    num++;
                    currentY += Convert.ToInt32(perPixel);
                }

                p.Dispose();
            }
        }
        Panel pRedLine = new Panel();
        public void DrawRedLine()//画出红线的位置
        {
            //Graphics g = this.PnlBigBoard.CreateGraphics();
            //g.Clear(PnlBigBoard.BackColor);
            //Pen p = new Pen(Color.Red, 1);
            //int x = Convert.ToInt32(DataTransfer.curTemplate.BoardLength * perPixel);
            //int y = Convert.ToInt32(DataTransfer.curTemplate.BoardHeight * perPixel);
            //g.DrawLine(p, x, 0, x, y);

            if (DataTransfer.curTemplate != null)
            {
                int x = DataTransfer.inchBoardWidth;
                int y = DataTransfer.inchBoardHeight;


                pRedLine.Height = y;
                pRedLine.Location = new Point(x + 1, 0);
                this.PnlBigBoard.Controls.Add(pRedLine);
            }
        }

        public void InitCtlsLoaction()//调整相关控件位置
        {

            PnlBigBoard.Left = xtraTabControl1.Right + 25;
            PnlBigBoard.Top = this.toolStrip1.Height + 25;//new 25 表示用来画刻度的宽度 
            PnlBigBoard.Height = Convert.ToInt32(DataTransfer.curTemplate.BoardHeight * perPixel);

            pnlPhotoInfo.Top = PnlBigBoard.Bottom + 7;//new

            pnlPhotoInfo.Height = this.Height - pnlPhotoInfo.Top - statusStrip1.Height - 36;
            cmbTemplate.Text = DataTransfer.curTemplate.TemplateName;
        }

        public void BindCmbTempate(List<Tb_paperTemplate_Info> paperTemplateList)//绑定模板下拉列表
        {
            cmbTemplate.Items.Clear();
            foreach (var pt in paperTemplateList)
            {
                cmbTemplate.Items.Add(pt.TemplateName);
            }
        }

        public void BindMainGrid(List<PhotoInfo_PinBan> _photosList)//绑定“下面”主要的照片列表
        {
            this.gridControl1.DataSource = _photosList;
            lblPhotoCount.Text = "   照片总数：" + _photosList.Count.ToString();
        }

        public void BindShopList(List<string> shopNameList)//绑定商户列表
        {
            //xtraTabControl1.SelectedTabPageIndex = 0;
            ////LBShop.Items.Clear();
            ////foreach (var shopName in shopNameList)
            ////{
            ////    LBShop.Items.Add(shopName);
            ////}
        }

        public void BindPhotoSizeList(List<string> photoSizeList)//绑定照片尺寸列表
        {
            LBSize.Items.Clear();
            LBSize.DataSource = photoSizeList;
            LBSize.DisplayMember = "";
        }

        public void BindClearGrid(List<PhotoInfo_PinBan> clearBoxPhotos)//绑定清柜的照片列表
        {
            this.gridControl1.DataSource = clearBoxPhotos;
        }

        public string SelectTemplateName//选择的模板名称
        {
            get
            {
                if (cmbTemplate.SelectedItem == null)
                {
                    return null;
                }
                else
                {
                    return cmbTemplate.SelectedItem.ToString().Trim();
                }

            }
        }
        private bool _showAllPapertypePhotos;
        public bool ShowAllPapertypePhotos
        {
            get
            {
                return _showAllPapertypePhotos;
            }
            set
            {
                _showAllPapertypePhotos = chkShowAllPaperTypePhotos.Checked;
            }
        }

        public void ShowStatusMsg(string msg)//显示状态信息
        {
            lblMes.Text = msg;
        }

        public void ShowPic(PicView picV)//将图片显示在大纸上
        {
            PnlBigBoard.Controls.Add(picV);
        }

        public void BindShopList(List<BindShopInfo> listBindShop)
        {
            xtraTabControl1.SelectedTabPageIndex = 0;
            LBShop.Nodes.Clear();
            if (listBindShop.Count > 0)
            {
                LBShop.Nodes.Add(new TreeNode() { Text = "全部", Tag = 1 });
            }
            TreeNode selNode = null;
            bool isPriority = false;
            foreach (var shop in listBindShop)
            {
                isPriority = false;
                TreeNode shopNode = LBShop.Nodes.Add(shop.ShopName);

                shopNode.Tag = 1;
                if (!string.IsNullOrEmpty(_selectShopName))
                {
                    if (shopNode.Text == _selectShopName)
                    {
                        selNode = shopNode;
                    }
                }

                foreach (var ctm in shop.Ctms)
                {
                    bool ctmIsPriority = false;
                    TreeNode ctmNode = shopNode.Nodes.Add(ctm.CtmName);
                    ctmNode.Tag = 2;
                    if (!string.IsNullOrEmpty(_selectCtmName))
                    {
                        if (ctmNode.Text == _selectCtmName)
                        {
                            selNode = ctmNode;
                        }
                    }

                    foreach (var orderProductBind in ctm.OrderList)
                    {
                        TreeNode orderNode = ctmNode.Nodes.Add(orderProductBind.OrderProductName);
                        if (orderProductBind.IsPriority)
                        {
                            ctmIsPriority = true;
                            isPriority = true;
                            orderNode.BackColor = Color.Tomato;
                            orderNode.ForeColor = Color.YellowGreen;
                        }
                        orderNode.Tag = 3;
                        if (!string.IsNullOrEmpty(_selectOrderProductName))
                        {
                            if (orderProductBind.OrderProductName == _selectOrderProductName)
                            {
                                selNode = orderNode;
                            }
                        }
                    }
                    if (ctmIsPriority)
                    {
                        ctmNode.BackColor = Color.Tomato;
                        ctmNode.ForeColor = Color.YellowGreen;
                    }
                }
                if (isPriority)
                {
                    shopNode.BackColor = Color.Tomato;
                    shopNode.ForeColor = Color.YellowGreen;
                }
            }
            if (selNode != null)
            {
                selNode.Expand();
            }



            LBShop.SelectedNode = selNode;
            _presenter.FilterShop();
        }


        private string _selectCtmName;
        public string SelectCtmName
        {
            get
            {
                return _selectCtmName;
            }
            set
            {
                _selectCtmName = value;
            }
        }

        private string _selectOrderProductName;
        public string SelectOrderProductName
        {
            get
            {
                return _selectOrderProductName;
            }
            set
            {
                _selectOrderProductName = value;
            }
        }

        public List<int> SelectPhotoIdList
        {
            get
            {
                List<int> photoIdList = new List<int>();
                foreach (var rowIndex in gridView1.GetSelectedRows())
                {
                    photoIdList.Add(((PhotoInfo_PinBan)gridView1.GetRow(rowIndex)).PhotoId);
                }

                return photoIdList;
            }
        }

        #endregion

        private void checkBox2_CheckedChanged_1(object sender, EventArgs e)//改变条码的方向
        {
            //_presenter.ClearBigBoard();
            if (checkBox2.Checked)
            {
                //条码在下向
                DataTransfer.BarCodeStatus = 0;
            }
            else
            {
                DataTransfer.BarCodeStatus = 1;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "左方")
            {
                //条码在下向
                DataTransfer.BarCodeStatus = 1;
            }
            else if (comboBox1.Text == "上方")
            {
                DataTransfer.BarCodeStatus = 0;
            }
            else
            {
                DataTransfer.BarCodeStatus = 2;
            }
        }

        private void cmboxOrderNum_TextChanged(object sender, EventArgs e)
        {
            //改变显示的数量
            int changingNum = 30;//默认是30
            try
            {
                changingNum = Convert.ToInt32(cmboxOrderNum.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入数字");
                cmboxOrderNum.SelectedIndex = 0;
                return;
            }
            if (changingNum <= 0)
            {
                MessageBox.Show("请输出大于0的数字");
                return;
            }

            DataTransfer.ShowOrderNum = changingNum;
            ModifyConfigurationOP.SetConfig("PBorderNum", changingNum.ToString());

        }

        private void cmboxOrderNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmboxOrderNum.Text == "全部")
            {
                DataTransfer.ShowOrderNum = -1;//代表全部
            }
            else
            {
                try
                {
                    DataTransfer.ShowOrderNum = Convert.ToInt32(cmboxOrderNum.Text);
                }
                catch (Exception)
                {
                    DataTransfer.ShowOrderNum = 30;
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e) //在照片列表中双击一张图片就自动将这张照片放到大板中
        {
            //双击一照片就自动将图片放到大板上
            PhotoInfo_PinBan selectedPi = gridView1.GetFocusedRow() as PhotoInfo_PinBan;
            if (selectedPi != null)
            {
                if (selectedPi.IsInBoard)
                {
                    return;
                }
                List<PhotoInfo_PinBan> templist = new List<PhotoInfo_PinBan>();
                templist.Add(selectedPi);

                _presenter.NewDrawPic(templist, new Point(0, 0));
            }
        }

        private void LBSize_DoubleClick(object sender, EventArgs e)//双击某一尺寸信息，就自动将这种尺寸的全部照片放到大板中, 一次三张
        {
            _presenter.AutoSelectedCurGridThreePicToBoard();
        }

        private void timerReushBoardStatus_Tick(object sender, EventArgs e)
        {
            RushBoardStatus();
        }

        private void RushBoardStatus()
        {
            this.pnlBoardStatus.Controls.Clear();

            //从数据库中获取目前的大板状态的数量
            int lblIndex = 0, lblWidth = 18;
            Point lblLocation = new Point(5, 5);
            var listMsg = BoardStatsMonitorBLL.GetAllBoardStausAndQutiy();
            foreach (var msg in listMsg)
            {
                Label lbl = new Label();
                lbl.AutoSize = true;
                lbl.Name = "lbl" + lblIndex;
                lbl.Text = msg;
                if (lblIndex != 0)
                {
                    lblLocation = new Point(5, lblLocation.Y + lblWidth + 5);
                }
                lblIndex++;
                lbl.Location = lblLocation;
                if (msg.Contains("错误"))
                {
                    lbl.ForeColor = Color.Red;
                }
                pnlBoardStatus.Controls.Add(lbl);
            }
        }

        private void comboBoxSizeUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawScaleLine();
            Properties.Settings.Default.SizeUint = comboBoxSizeUnit.SelectedIndex;
            Properties.Settings.Default.Save();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex == 0)
            {
                perPixel = DataTransfer.perPix = 10;

            }
            else
            {
                perPixel = DataTransfer.perPix = 4;
            }
            
            DrawScaleLine();
            cmbTemplate_DropDownClosed(sender, e);
        }

        private void chkShowAllPaperTypePhotos_CheckedChanged(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this._showAllPapertypePhotos = chkShowAllPaperTypePhotos.Checked;
            try
            {
                _presenter.RushBindPhotos(chkShowAllPaperTypePhotos.Checked);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
